package Zoo.View.ZoomanView;

import Zoo.Entity.ZoomenEntity1;
import Zoo.Handler.ZoomenView1Handler;
import Zoo.Impl.ZoomenService1Impl;
import Zoo.Service.ZoomenService1;
import Zoo.Util.TableDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;
/**
 * 动物饲养员登录后看到的动物信息界面
 */


public class ZoomenView1 extends JFrame {
    JPanel northPanel  =new JPanel();
    JButton addBtn =new JButton("增加");
    JButton updateBtn =new JButton("修改");
    JButton delBtn =new JButton("删除");
    JTextField searchTxt=new JTextField(15);
    JButton searchBtn= new JButton("查询");
    private String zoomenName1;
    JPanel southPanel =new JPanel();
//    JButton preBtn =new JButton("上一页");
//    JButton nextBtn =new JButton("下一页");



//    private int pageNow=1;  //当前是第几页
//    private int pageSize=10;  //一页显示多少条记录

    ZoomenView1Handler zoomenView1Handler;
//    public void setPageNow(int pageNow) {
//        this.pageNow = pageNow;
//
//    }
    public ZoomenView1(String zoomenName1){

        super("动物园动物饲养员界面");
        Container contentPane= getContentPane();
        zoomenView1Handler =new ZoomenView1Handler(this);

        //放置北边的组件
        layoutNorth(contentPane);
        this.zoomenName1=zoomenName1;
        //放置南边的组件
//        layoutSouth(contentPane);

//
//        Vector<Vector<Object>> data=new Vector<>();
//
//        Vector<Object> rowVector1=new Vector<>();
//        rowVector1.addElement("张三");
//        rowVector1.addElement("1");
//        rowVector1.addElement("武汉");
//
//        Vector<Object> rowVector2=new Vector<>();
//        rowVector2.addElement("李四");
//        rowVector2.addElement("2");
//        rowVector2.addElement("长安");
//
//        Vector<Object> rowVector3=new Vector<>();
//        rowVector3.addElement("王五");
//        rowVector3.addElement("3");
//        rowVector3.addElement("江门");
//
//        data.addElement(rowVector1);
//        data.addElement(rowVector2);
//        data.addElement(rowVector3);


        Vector<Vector<Object>> data = getVectors();


        //tablemodel
        ZoomenTableModel1 zoomenTableModel1 = ZoomenTableModel1.assembleModel(data);
        //jtable
        JTable jTable=new JTable(zoomenTableModel1);
        //设置表头
        JTableHeader tableHeader=jTable.getTableHeader();
        tableHeader.setFont(new Font(null,Font.BOLD,16));
        tableHeader.setForeground(Color.RED);

        //设置表格体
        jTable.setFont(new Font(null,Font.PLAIN,14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);
        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns=zoomenTableModel1.getColumns();
        ZoomenCellRender1 render1=new ZoomenCellRender1();
        for(int i=0;i<columns.size();i++){
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render1);
        }


        JScrollPane jScrollPane=new JScrollPane(jTable);
        contentPane.add(jScrollPane);

        setSize(700,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setResizable(false);
        setVisible(true);
    }

    //主界面和查询功能
    private Vector<Vector<Object>> getVectors() {
//        UserService userService=new UserServiceImpl();
//        UserEntity userEntity=new UserEntity();
        ZoomenService1 zoomenService1=new ZoomenService1Impl();
        ZoomenEntity1 zoomenEntity1=new ZoomenEntity1();
        //获取查询内容
        zoomenEntity1.setSearchKey(searchTxt.getText().trim());
        TableDTO tableDTO = zoomenService1.retrieveZoomen1(zoomenEntity1,zoomenName1);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }

    //放置北边组件
    private void layoutNorth(Container contentPane) {
        //增加事件监听
        addBtn.addActionListener(zoomenView1Handler);
        updateBtn.addActionListener(zoomenView1Handler);
        delBtn.addActionListener(zoomenView1Handler);
        searchBtn.addActionListener(zoomenView1Handler);

        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel,BorderLayout.NORTH);
    }


    //查询功能的实现
    public void reloadTable(){
        Vector<Vector<Object>> data = getVectors();
        ZoomenTableModel1 zoomenTableModel1=new ZoomenTableModel1();
        zoomenTableModel1.updateModel(data);

    }

    //分页
//    private void layoutSouth(Container contentPane) {
//        southPanel.add(preBtn);
//        southPanel.add(nextBtn);
//        contentPane.add(southPanel,BorderLayout.SOUTH);
//    }

//    public static void main(String[] args) {
//        new ZoomenView1();
//    }
}
class ZoomenCellRender1 extends DefaultTableCellRenderer{
    //在每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(row%2==0){
            setBackground(Color.LIGHT_GRAY);
        }else{
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tablemodel
class ZoomenTableModel1 extends DefaultTableModel{
    static Vector<String> columns=new Vector<>();
    static{
        columns.addElement("动物id");
        columns.addElement("动物名称");
        columns.addElement("种类");
        columns.addElement("介绍");
        columns.addElement("年龄");
        columns.addElement("健康状况");
        columns.addElement("园区id");
    }

    public ZoomenTableModel1(){
        super(null,columns);
    }

    private static ZoomenTableModel1 zoomenTableModel1=new ZoomenTableModel1();

    public static ZoomenTableModel1 assembleModel(Vector<Vector<Object>> data){
        zoomenTableModel1.setDataVector(data,columns);
        return zoomenTableModel1;
    }


    public static void updateModel(Vector<Vector<Object>> data){
        zoomenTableModel1.setDataVector(data,columns);
    }

    public static Vector<String> getColumns() {
        return columns;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }



}